<?php


class  BaseController
{

    public function success($str,$url){
        $notice = ['notice'=>$str,'url'=>$url];
        $this->display('success',$notice,true);

    }

    public function error($str,$url){

        $notice = ['notice'=>$str,'url'=>$url];
        $this->display('error',$notice,true);
    }

    public function redirect($url){

        if(count(explode('/',$url)) == 3){
            $url = "http://" . $_SERVER['SERVER_NAME']. $url;
        }else{
            $url = "http://" . $_SERVER['SERVER_NAME']."/".DEFAULT_MODULE."/" .$url;
        }

        header("location:$url");
    }

    public function display($view = '',$data=[],$sys = false){

        $backtrace = debug_backtrace();
        array_shift($backtrace);
        if($data) extract($data);

        if(!$sys){
            $class = strtolower($backtrace[0]['class']);
            $class = str_replace('controller','',$class);
            $filename = strtolower($backtrace[0]['function']);
            $module = ucwords(DEFAULT_MODULE);
            if(!$view){
                $path = BASE_URL . '/' . APP_PATH . '/' . $module  . '/View/' .ucwords($class).'/'. $filename .'.html';
            }else{

                $path = BASE_URL . '/' . APP_PATH . '/' . $module  .  '/View/'.ucwords($class).'/'. $view .'.html';
            }
        }else{
            $path = BASE_URL . '/' . APP_PATH . '/' . 'Public'  .  '/notice/'. $view .'.html';
        }

        if(is_file($path)){
            //加载
            require_once $path;
            exit;
        }else{
            echo '没有找到指定的模板文件';
        }
    }
    public function checkCode($code){
        if(strtolower($_SESSION['captcha']) != strtolower(trim($code))){
            $this->error('验证码错','user/login');
        }
    }
    public function yanzhengma(){

        /**
         * 字母+数字的验证码生成
         */

        $image = imagecreatetruecolor(100, 30);

        $bgcolor = imagecolorallocate($image, 255, 255, 255);

        imagefill($image, 0, 0, $bgcolor);


        $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        $captcha = "";
        for ($i = 0; $i < 4; $i++) {
            $fontsize = 10;
            $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            $x = ($i * 100 / 4) + mt_rand(5, 10);
            $y = mt_rand(5, 10);
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }
        $_SESSION["captcha"] = $captcha;

        for ($$i = 0; $i < 200; $i++) {
            $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
        }

        for ($i = 0; $i < 3; $i++) {
            $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
        }
        header('content-type:image/png');
        imagepng($image);
        imagedestroy($image);
    }





}